package com.vinku.agent.attach;

import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;

import java.util.List;

/**
 * @Author Vinku
 * @Date 2023/2/13
 * @Description
 */
public class AttachMain {

    public static void main(String[] args) throws Exception {
        List<VirtualMachineDescriptor> list = VirtualMachine.list();
        for (VirtualMachineDescriptor virtualMachineDescriptor : list) {
            System.out.println(virtualMachineDescriptor.id() + '\t' +virtualMachineDescriptor.displayName());
            if (virtualMachineDescriptor.displayName().startsWith("com.vinku.app")) {
                // 附着在目标JVM上
                VirtualMachine vm = VirtualMachine.attach(virtualMachineDescriptor.id());
                // 加载agent
                vm.loadAgent("D:\\vinku-agent\\my-agent\\target\\my-agent-1.0-SNAPSHOT.jar", "a=1");
                break;
            }
        }
    }

}
